﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class VertitalScrollCircle : HorizontalScrollCircle
{

    protected override void OnRefrashPos(ScrollCircleItem[] items, RectTransform content)
    {
        //Vector3 swl = scrollview.transform.TransformPoint(new Vector3(rt.rect.x, rt.rect.y + rt.rect.height, 0));
        //Vector3 swr = scrollview.transform.TransformPoint(new Vector3(rt.rect.x + rt.rect.width + m_Space, rt.rect.y - m_Space, 0));
        ////Vector3 lpt = content.InverseTransformPoint(rt.TransformPoint(new Vector3(rt.rect.x, rt.rect.y + rt.rect.height, 0)));
        ////Vector3 lpb = content.InverseTransformPoint(rt.TransformPoint(new Vector3(rt.rect.x + rt.rect.width, rt.rect.y, 0)));
        //for (int i = 0; i < items.Length; i++)
        //{
        //    ScrollCircleItem item = items[i];
        //    //Vector2 sz = item.GetSize();
        //    //Vector2 pos = item.GetPos();
        //    //if (pos.y + sz.y  + m_Space > lpb.y && pos.y - m_Space - sz.y*2 < lpt.y)
        //    Vector2 sz = content.TransformPoint(item.GetSize());
        //    Vector2 pos = content.TransformPoint(item.GetPos());
        //    if (pos.y - sz.y < swl.y && pos.y > swr.y)
        //        item.OnShow();
        //    else
        //        item.OnHide();
        //}

        for (int i = 0; i < items.Length; i++)
        {
            ScrollCircleItem item = items[i];
            Vector3 llb = rt.InverseTransformPoint(content.TransformPoint(new Vector3(item.rect.x,
                item.rect.y - item.rect.height * 0.5f - Mathf.Abs(m_Space), 0)));
            Vector3 lrt = rt.InverseTransformPoint(content.TransformPoint(new Vector3(item.rect.x + item.rect.width,
                item.rect.y + item.rect.height * 1.5f + Mathf.Abs(m_Space), 0)));
            Rect itemt = new Rect(llb.x, llb.y, Mathf.Abs(lrt.x - llb.x), Mathf.Abs(lrt.y - llb.y));
            if (rt.rect.Overlaps(itemt))
                item.OnShow();
            else
                item.OnHide();
        }

    }



    protected override void OnRefrashSize(ScrollCircleItem[] items, RectTransform content)
    {
        Vector2 SIZE = new Vector2(0, 0);
        for (int i = 0; i < items.Length; i++)
        {
            Vector2 sz = items[i].GetSize();
            SIZE.y += sz.y;
            SIZE.x = Mathf.Max(SIZE.x, sz.x);
        }
        if (items.Length > 0)
            SIZE.y += ((items.Length - 1) * m_Space);
        CSIZE = SIZE;
        CSIZE.x = SIZE.x + m_Padding.left + m_Padding.right;
        CSIZE.y = SIZE.y + m_Padding.top + m_Padding.bottom;
        CSIZE = new Vector2(Mathf.Max(0, CSIZE.x), Mathf.Max(0, CSIZE.y));
        content.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, CSIZE.y);
        Vector2 pltPos = new Vector2(0, 0);
        Vector2 prbPos = new Vector2(CSIZE.x, -CSIZE.y);

        Vector2 cltPos = new Vector2(pltPos.x + m_Padding.left, pltPos.y - m_Padding.top);
        Vector2 crbPos = new Vector2(prbPos.x - m_Padding.right, prbPos.y + m_Padding.bottom);

        int offset = 1;
        Vector2 nextPos = new Vector2(cltPos.x, cltPos.y);
        switch (m_ChildAlignment)
        {
            case TextAnchor.UpperLeft:
                offset = 1;
                nextPos = new Vector2(cltPos.x, cltPos.y);
                break;
            case TextAnchor.UpperCenter:
                offset = 0;
                nextPos = new Vector2(cltPos.x + (crbPos.x - cltPos.x) / 2.0f, cltPos.y);
                break;
            case TextAnchor.UpperRight:
                offset = -1;
                nextPos = new Vector2(crbPos.x, cltPos.y);
                break;
            case TextAnchor.LowerLeft:
                offset = 1;
                nextPos = new Vector2(cltPos.x, crbPos.y + SIZE.y);
                break;
            case TextAnchor.LowerCenter:
                offset = 0;
                nextPos = new Vector2(cltPos.x + (crbPos.x - cltPos.x) / 2.0f, crbPos.y + SIZE.y);
                break;
            case TextAnchor.LowerRight:
                offset = -1;
                nextPos = new Vector2(crbPos.x, crbPos.y + SIZE.y);
                break;
            case TextAnchor.MiddleLeft:
                offset = 1;
                nextPos = new Vector2(cltPos.x, cltPos.y - ((cltPos.y - crbPos.y) / 2.0f - SIZE.y / 2.0f));

                break;
            case TextAnchor.MiddleCenter:
                offset = 0;
                nextPos = new Vector2(cltPos.x + (crbPos.x - cltPos.x) / 2.0f, cltPos.y - ((cltPos.y - crbPos.y) / 2.0f - SIZE.y / 2.0f));
                break;
            case TextAnchor.MiddleRight:
                offset = -1;
                nextPos = new Vector2(crbPos.x, cltPos.y - ((cltPos.y - crbPos.y) / 2.0f - SIZE.y / 2.0f));
                break;
        }

        for (int i = 0; i < items.Length; i++)
        {
            Vector2 sz = items[i].GetSize();
            items[i].SetPos(new Vector2(nextPos.x + offset * sz.x / 2.0f - sz.x / 2.0f, nextPos.y));
            nextPos.y = nextPos.y - sz.y - m_Space;
        }
    }
}
